package BLC

import (
	"fmt"
	"time"
)

type Block struct{
	//时间戳，创建区块的时间
	TimeStamp int64
	//上一个区块的hash，父hash
	PreviewHash []byte
	//交易数据
	Data []byte
	//hash当前区块的hash
	Hash []byte
	//区块的nonce的值
	Nonce int
}



func NewBlock(data string,previewHash []byte)*Block{
	b:=&Block{
		TimeStamp:time.Now().Unix(),
		PreviewHash:previewHash,
		Data:[]byte(data),
		Hash:[]byte{},
		Nonce:0,
	}
	p:=NewProofOfWork(b)
	nonce,hash:=p.Run()
	b.Hash=hash
	b.Nonce=nonce

	return b
}

func NewGenisBlock()*Block{
	return NewBlock("Genis Block",[]byte{})
}

func (b *Block)Print(){
	fmt.Printf("时间戳是:%s\n",time.Unix(b.TimeStamp,0).Format("2006-01-02 15:04:05.999999999"))
	fmt.Printf("前一个区块的hash是:%x\n",b.PreviewHash)
	fmt.Printf("交易的数据是:%s\n",b.Data)
	fmt.Printf("当前区块的hash是:%x\n",b.Hash)
	fmt.Printf("当前区块的Nonce是:%d\n",b.Nonce)
}
